import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
import vueJsx from '@vitejs/plugin-vue-jsx'
import setting from './src/settings'
import AutoImport from "unplugin-auto-import/vite"
import { loadEnv } from 'vite'
export default ({ command, mode }) => {
  let env = loadEnv(mode, process.cwd())
  return {
    base: setting.viteBasePath,
    define: {
      'process.env': {}
    },
    clearScreen: false,
    server: {
      hmr: { overlay: false }, // 禁用或配置 HMR 连接 设置 server.hmr.overlay 为 false 可以禁用服务器错误遮罩层
      // 服务配置
      port: 8888, // 类型： number 指定服务器端口;
      open: false, // 类型： boolean | string在服务器启动时自动在浏览器中打开应用程序；
      cors: true, // 类型： boolean | CorsOptions 为开发服务器配置 CORS。默认启用并允许任何源
      proxy: {
        '/api': {
          // todo 请不要修改这个，否则demo演示将无法使用
          target: 'https://mock.apifox.cn/m1/1457328-0-default',
          changeOrigin: true
        }
      }
    },
    plugins: [
      vue(),
      vueJsx(),
      AutoImport ({
        imports: ["vue", "vuex", "vue-router"], // 自动导入vue和vue-router相关函数
        dts: true // 生成 `auto-import.d.ts` 全局声明
      }),
      legacy({
        targets: ['ie >= 11'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime']
      })
    ],
    build: {
      // 消除打包大小超过500kb警告
      chunkSizeWarningLimit: 2000,
      //build assets Separate
      assetsDir: 'static/assets',
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        }
      }
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        //remove i18n waring
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      }
      //why remove it , look for https://github.com/vitejs/vite/issues/6026
      // extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.mjs']
    },
    css: {
      preprocessorOptions: {
        //define global scss variable
        scss: {
          additionalData: `@import "@/styles/comm/variables.scss";`
        }
      },
      postcss: {
        plugins: [
          {
            // 去除build模式charset警告
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ],
      },
    },
  }
}
